package com.liyu.listener;

import com.liyu.config.DirectComment;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author 李誉
 * @date 2022-07-04 22:28
 * @description 直连队列消费者
 */
@Component
@Slf4j
public class DirectConsumer {

    @RabbitListener(queues = DirectComment.DIRECT_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("队列{}消费了一条消息：{}", DirectComment.DIRECT_QUEUE, msg);
        // 手动应答，注意默认是自动应答，所以这里不需要设置手动应答
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
